import { Get, Patch, Post } from 'para-lib';

const baseUrl = 'http://192.168.1.41:27018/api/';
// const baseUrl = 'http://localhost:3000/api/'
const api: Record<string, string> = {
  FORM: 'hackathon/form',
  TABLE: 'hackathon/table',
  CREATE_SCREENSHOT: 'common/generateBlob',
  CREATE_TABLE: 'hackathon/table/createTable'
};
for (let k in api) {
  api[k] = baseUrl + api[k];
}
const toLowerCase = (str: string) => {
  if(!str) return ''
  return str.toLowerCase()
}
/**
 * 创建设计器表单
 * @param json      表单结构
 * @param tableName  映射表名
 */
export const createForm = async (json: Record<string, any>, tableName: string, tableAlias: string) => {
  const { data, err } = await Post({
    url: api.FORM,
    data: {
      json: json,
      tableName: toLowerCase(tableName),
      tableAlias,
    },
  });
  if (err) return null;
  return data;
};

/**
 * 更新设计器表单
 * @param json      表单结构
 * @param tableName  映射表名
 */
export const updateForm = async (id: string, json: Record<string, any>, tableName: string, tableAlias: string) => {
  const { data, err } = await Patch({
    url: `${api.FORM}/${id}`,
    data: {
      json: json,
      tableName: toLowerCase(tableName),
      tableAlias
    },
  });
  if (err) return null;
  return data;
};

/**
 * 获取表单
 * @param id  表单id
 */
export const getFormById = async (id: string) => {
  const { data, err } = await Get({
    url: `${api.FORM}/${id}`,
  });
  if (err) return null;
  return data;
};

/**
 * 创建表
 * @param formData 表结构
 * @param name     表名称
 */
export const createTable = async (formData: Record<string, any>, name: string) => {
  const { data, err } = await Post({
    url: `${api.CREATE_TABLE}/${toLowerCase(name)}`,
    data: formData,
  });
  if (err) return null;
  return data;
};

/**
 * 更新表结构字段
 * @param formData 表结构
 * @param name     表名称
 */
export const updateTable = async (formData: Record<string, any>, name: string) => {
  const { data, err } = await Patch({
    url: `${api.TABLE}/${toLowerCase(name)}`,
    data: formData,
  });
  if (err) return null;
  return data;
};

/**
 * 插入新的表单数据
 * @param name  表单名称
 */
export const saveTableData = async (name: string, formData: Record<string, any>) => {
  const { data, err } = await Post({
    url: `${api.TABLE}/insertOne/${toLowerCase(name)}`,
    data: formData,
  });
  if (err) return null;
  return data;
};

/**
 * 更新表数据
 * @param name  表单名称
 * @param id  表id
 */
export const updateTableData = async (name: string, id: string, formData: Record<string, any>) => {
  const { data, err } = await Post({
    url: `${api.TABLE}/${toLowerCase(name)}`,
    data: {id,  content: formData}
  });
  if (err) return null;
  return data;
};

/**
 * 根据table id 获取表格数据
 * @param name  表单名称
 * @param name  表id
 */
export const getTableData = async (name: string, id: string) => {
  const { data, err } = await Post({
    url: `${api.TABLE}/${toLowerCase(name)}`,
    data: {id}
  });
  if (err) return null;
  return data;
};

/**
 * 创建截图
 * @param url
 */
export const createScreenShot = async (url: string) => {
  const { data, err } = await Post({
    url: api.CREATE_SCREENSHOT,
    data: {
      url,
      blobUrl: '',
    },
  });
  if (!err) return;
  return data;
};
